<div id="formDiv">

</div>
<style>
    #inquiryOrderInformationForm .layui-form-label {
        width: 110px;
    }

    #inquiryOrderInformationForm .layui-input-block {
        margin-left: 130px;
    }
</style>
<script type="text/html" id="formContent">
    <form lay-filter="inquiryOrderInformationForm" id="inquiryOrderInformationForm" class="layui-form open-form">
        <div class="open-model" id="inquiryOrderInformationFormContent">
            <div class="open-auto">
                <div class="layui-form-item tableContent">
                    <table class="layui-table" id="purchasePlanCheckFormTable"
                           lay-filter="purchasePlanCheckFormTable"></table>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">关闭</button>
                {{# if(!['acceptance','details'].includes(d.eventName)){ }}
                <button class="layui-btn" lay-filter="inquiryOrderInformationSubmit" type="button" lay-submit>提交
                </button>
                {{# } }}
            </div>
        </div>

    </form>
</script>
s
<script type="text/html" id="purchasePlanCheckFormTableBar">
    <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" type="button">删除</button>
</script>
<script>

    layui.use(['layer', 'form', 'table', 'admin', 'laydate', 'upload', 'laytpl'], async function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        let laydate = layui.laydate;
        let laytpl = layui.laytpl;
        const nowData = {...parentData}

        // const loadingIndex = layer.load();
        laytpl(formContent.innerHTML).render({...nowData}, function (html) {
            $('#formDiv').after(html);
        });
        if (nowData.eventName === 'view') {
            admin.setFormDisable('#inquiryOrderInformationFormContent', true)
        }
        table.render({
            elem: '#purchasePlanCheckFormTable',
            page: false,
            limit: 999999,
            data: [...nowData.data],
            cols: [[{type: 'numbers'},
                {field: 'productName', align: 'center', title: '物料名称',},
                {field: 'skuCode', align: 'center', title: '物料编号',},
                {field: 'specifications', align: 'center', title: '规格型号',},
                {field: 'productCategory', align: 'center', title: '物料分类',},
                {field: 'unitName', align: 'center', title: '采购单位',},
                {field: 'checkNumber', title: '领料计划数量', align: 'center',},
                {
                    field: 'surplusQuantity',
                    align: 'center',
                    title: '剩余领料数量',

                },
                {
                    field: 'quantity',
                    title: '本次请购数量',
                    align: 'center',
                    templet: admin.setTableInput('quantity'),
                    hide: ['view'].includes(nowData.eventName)
                },
                // {
                //     title: '税率(%)',
                //     align: 'center',
                //     // templet: '#rate',
                //     templet: admin.setTableInput('rate'),
                //     hide: ['view'].includes(nowData.eventName)
                // },
                {
                    title: '已请购领料数量', align: 'center', width: 140, templet(d) {
                        return Number(d.checkNumber) - Number(d.surplusQuantity);
                    }, hide: ['generate'].includes(nowData.eventName)
                },
                {
                    align: 'center',
                    toolbar: '#purchasePlanCheckFormTableBar',
                    title: '操作',
                    hide: ['view'].includes(nowData.eventName)
                }
            ]],
            done: function (res, curr, count) {
                admin.tableInputChange('purchasePlanCheckFormTable')
            },
            error: function () {
                console.log('请求失败');
            }
        });


        form.on('submit(inquiryOrderInformationSubmit)', function (d) {
            if (table.cache['purchasePlanCheckFormTable'].some(v => !v.quantity||(Number(v.quantity) - Number(v.surplusQuantity))> 0)) {
                layer.msg('本次请购数量不能大于剩余领料数量', {icon: 2, time: 1000});
                return false;
            } else {
                const formData = {
                    purchasePlanCheckId: nowData.id,
                    purchaseRequisitionsDetails: table.cache['purchasePlanCheckFormTable'].map(v => {
                        v.planCheckDetailId = v.id
                        return v
                    })
                };
                admin.req('enterprise/purchase/admin/purchasePlanCheck/addPurchaseRequisitions', JSON.stringify(formData), function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1, time: 500});
                        table.reload('purchasePlanCheckTable');
                        layer.closeAll('page');
                    } else {
                        layer.msg(res.msg, {icon: 2, time: 1000});
                    }
                }, 'POST', false, "application/json");
            }


            return false;
        })

        table.on('tool(purchasePlanCheckFormTable)', function (obj) {
            var d = obj.data;  // 获取当前行的数据
            var layEvent = obj.event;  // 获取事件名（edit 或 del）
            if (obj.tr) {
                obj.tr.on('click', function (e) {
                    e.preventDefault(); // 防止默认行为
                });
            }
            if (layEvent == 'del') {
                const index = $(obj.tr[0]).data('index')
                let list = table.cache['purchasePlanCheckFormTable'].filter(v => {
                    return v['LAY_TABLE_INDEX'] != index
                })
                table.reload('purchasePlanCheckFormTable', {
                    data: list
                })
            }

        });
    })

</script>
